package sourceagile.webservices.subversion;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sourceagile.server.subversionFileRepository.edit.SaveFeatureFile;
import sourceagile.server.utilities.FileNameGenerator;
import sourceagile.webservices.GetCallback;

@SuppressWarnings("serial")
public class SaveSubversionFeatureServlet extends HttpServlet {

	// http://localhost:8080/LiveSourceWebServices/SaveSubversionFeatureService
	// ?repositoryURL=https://tictactoe4j.googlecode.com/svn/trunk
	// &username=alline.oliveira&password=bD8Km8se2rV9
	// &featureName=Test&priority=1

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String repositoryURL = request.getParameter("repositoryURL");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		String featureName = request.getParameter("featureName");

		String featureDescription = request.getParameter("featureDescription");

		String priority = request.getParameter("priority");

		String estimate = request.getParameter("estimate");

		String status = request.getParameter("status");

		String fileName = FileNameGenerator.compactName(featureName)
				+ ".feature";

		SaveFeatureFile.save(repositoryURL, username, password, "features",
				null, fileName, featureName, featureDescription, priority,
				estimate, status);

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), "OK");

		response.getWriter().println(answer);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		String repositoryURL = request.getParameter("repositoryURL");

		String username = request.getParameter("username");

		String password = request.getParameter("password");

		String featureName = request.getParameter("featureName");

		String featureDescription = request.getParameter("featureDescription");

		String priority = request.getParameter("priority");

		String estimate = request.getParameter("estimate");

		String status = request.getParameter("status");

		String fileName = FileNameGenerator.compactName(featureName)
				+ ".feature";

		SaveFeatureFile.save(repositoryURL, username, password, "features",
				null, fileName, featureName, featureDescription, priority,
				estimate, status);

		String answer = GetCallback.getCallback(
				request.getParameter("callback"), "OK");

		response.getWriter().println(answer);
	}
}
